package com.snap.vseries.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;

import com.snap.vseries.R;

public class ThreeCheckBox extends AppCompatCheckBox {
    public static final int STATE_NONE = 1;
    public static final int STATE_PART = 2;
    public static final int STATE_ALL = 3;
    final int[] state = {
            R.attr.state_type
    };
    final int[] state_none = {
            R.attr.three_state_none
    };
    final int[] state_part = {
            R.attr.three_state_part
    };
    final int[] state_all = {
            R.attr.three_state_all
    };
    private int checkState;

    public ThreeCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, state);
        if (array != null) {
            setState(array.getInt(R.styleable.ThreeCheckBox_state_type, 1));
            array.recycle();
        }
    }

    public int getState() {
        return checkState;
    }

    /**
     * 设置状态
     */
    public void setState(int checkState) {
        if (this.checkState != checkState) {
            this.checkState = checkState;
            refreshDrawableState();
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        switch (checkState) {
            case STATE_NONE:
                mergeDrawableStates(drawableState, state_none);
                break;
            case STATE_PART:
                mergeDrawableStates(drawableState, state_part);
                break;
            case STATE_ALL:
                mergeDrawableStates(drawableState, state_all);
                break;
        }
        return drawableState;
    }

}
